package com.sugar.MultiThread.a12deadlock;


import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 实现方式一：继承Thread
 *
 *
 * @author felix
 * @date 2025/2/26 09:32
 */
public class MyThread extends Thread {

    static Object objA = new Object();
    static Object objB = new Object();

    @Override
    public void run() {
        while(true){
            if("线程A".equals(getName())){
                synchronized (objA){
                    System.out.println("线程A持有锁A，现在申请锁B...");
                    synchronized (objB){
                        System.out.println("AB都有，天下我有...");
                    }
                }
            }else if("线程B".equals(getName())){
                synchronized (objB){
                    System.out.println("线程B持有锁B，现在申请锁A...");
                    synchronized (objA){
                        System.out.println("AB都有，天下我有...");
                    }
                }
            }
        }
    }

}
